from aiohttp import ClientSession

from random import choice
from string import ascii_letters
from os.path import exists

from capmonster_python import RecaptchaV2Task
from loguru import logger

import asyncio


capmonster = RecaptchaV2Task(input("Введите ключ от капмонстра -> "))


def get_proxy():
    return choice(proxies)


async def solve_captcha():
    task_id = capmonster.create_task(
            "https://robinhood.com/web3-wallet",
            "6LcNCM0fAAAAAJLML8tBF-AMvjkws6z4bfar9VFF")
    return (await capmonster.join_task_result_async(task_id)).get("gRecaptchaResponse")


async def register(referrer: str):
    async with ClientSession() as client:
        response = await client.post(
            "https://bonfire.robinhood.com/waitlist/web3_wallet/email/spot",
            json={
                "email": (
                    "".join([choice(ascii_letters) for _ in range(10)])
                    + "@gmail.com"),
                "referred_by": referrer,
                "token": await solve_captcha()
            },
            proxy=get_proxy())
    print(await response.json())


async def worker(referrer: str):
    while True:
        try:
            logger.info("Регистрация аккаунта")
            await register(referrer)
        except Exception:
            logger.exception("Ошибка при регистрации")
        else:
            logger.info("Успешная регистрация")


async def main():
    referrer = input("Реф код -> ")
    tasks = [asyncio.create_task(worker(referrer))
             for _ in range(int(input("Потоки -> ")))]

    await asyncio.gather(*tasks)


if __name__ == "__main__":
    if exists("proxies.txt"):
        proxies = open("proxies.txt", "r+").read().strip().split("\n")
    else:
        proxies = [None]

    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    loop.run_until_complete(main())
